<template>
  <div class="simple-carousel">
    <div class="carousel-inner" :style="transformStyle">
      <div 
        v-for="(article, index) in articles" 
        :key="article.articleId"
        class="carousel-item"
        :class="{ active: currentIndex === index }"
      >
        <router-link :to="`/article/${article.articleId}`" class="carousel-link">
          <div class="carousel-image">
            <img 
              v-if="article.coverImage" 
              :src="article.coverImage" 
              :alt="article.title"
              loading="lazy"
            >
            <div v-else class="image-placeholder">
              <i class="iconfont icon-picture"></i>
            </div>
          </div>
          <div class="carousel-content">
            <h3 class="carousel-title">{{ article.title }}</h3>
            <p class="carousel-desc">{{ article.summary || '暂无简介' }}</p>
          </div>
        </router-link>
      </div>
    </div>
    
    <button class="carousel-control prev" @click="prev">
      <i class="iconfont icon-left"></i>
    </button>
    <button class="carousel-control next" @click="next">
      <i class="iconfont icon-right"></i>
    </button>
    
    <div class="carousel-indicators">
      <button
        v-for="(item, index) in articles"
        :key="index"
        :class="{ active: currentIndex === index }"
        @click="goTo(index)"
      ></button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SimpleCarousel',
  props: {
    articles: {
      type: Array,
      default: () => []
    },
    interval: {
      type: Number,
      default: 3000
    }
  },
  data() {
    return {
      currentIndex: 0,
      timer: null
    }
  },
  computed: {
    transformStyle() {
      return {
        transform: `translateX(-${this.currentIndex * 100}%)`
      }
    }
  },
  mounted() {
    this.startAutoPlay()
  },
  beforeDestroy() {
    this.stopAutoPlay()
  },
  methods: {
    startAutoPlay() {
      this.timer = setInterval(() => {
        this.next()
      }, this.interval)
    },
    stopAutoPlay() {
      if (this.timer) {
        clearInterval(this.timer)
        this.timer = null
      }
    },
    prev() {
      this.currentIndex = 
        this.currentIndex <= 0 
          ? this.articles.length - 1 
          : this.currentIndex - 1
    },
    next() {
      this.currentIndex = 
        this.currentIndex >= this.articles.length - 1 
          ? 0 
          : this.currentIndex + 1
    },
    goTo(index) {
      this.currentIndex = index
    }
  }
}
</script>

<style scoped>
.simple-carousel {
  position: relative;
  width: 100%;
  overflow: hidden;
  border-radius: 6px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.carousel-inner {
  display: flex;
  transition: transform 0.5s ease;
}

.carousel-item {
  min-width: 100%;
  position: relative;
}

.carousel-link {
  display: block;
  height: 300px;
  position: relative;
  text-decoration: none;
  color: #fff;
}

.carousel-image {
  position: absolute;
  width: 100%;
  height: 100%;
}

.carousel-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.image-placeholder {
  width: 100%;
  height: 100%;
  background: #f0f2f5;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #c0c4cc;
  font-size: 50px;
}

.carousel-content {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 20px;
  background: linear-gradient(to top, rgba(0,0,0,0.7), transparent);
}

.carousel-title {
  font-size: 20px;
  margin-bottom: 10px;
  text-shadow: 0 1px 3px rgba(0,0,0,0.5);
}

.carousel-desc {
  font-size: 14px;
  margin: 0;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-shadow: 0 1px 2px rgba(0,0,0,0.5);
}

.carousel-control {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 40px;
  height: 40px;
  background: rgba(255,255,255,0.2);
  border: none;
  border-radius: 50%;
  color: white;
  font-size: 20px;
  cursor: pointer;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
}

.carousel-control:hover {
  background: rgba(255,255,255,0.4);
}

.carousel-control.prev {
  left: 15px;
}

.carousel-control.next {
  right: 15px;
}

.carousel-indicators {
  position: absolute;
  bottom: 15px;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  gap: 8px;
  z-index: 10;
}

.carousel-indicators button {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.5);
  padding: 0;
  cursor: pointer;
  transition: all 0.3s;
}

.carousel-indicators button.active {
  background: white;
  transform: scale(1.2);
}

@media (max-width: 768px) {
  .carousel-link {
    height: 200px;
  }
  
  .carousel-title {
    font-size: 16px;
  }
  
  .carousel-desc {
    font-size: 12px;
  }
}
</style>